﻿using mscook.core.domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;
namespace mscook.core.repositories
{
    public class CookBookRepository : Repository<CookBook>, ICookBookRepository
    {
        public IList<CookBook> GetByChef(Chef chef, int start, int limit)
        {
            var query = (from x in this.Session.Query<CookBook>() select x)
             .Where(o => o.IsDelete == false
                 && o.Chef == chef).OrderByDescending(o => o.CreateTime);
            return query.Skip(start).Take(limit).ToList();
        }

        public IList<CookBook> GetByKeyword(string keyword, int start, int limit,out long total)
        {
            var query = (from x in this.Session.Query<CookBook>() select x)
             .Where(o => o.IsDelete == false
                 &&(o.Name.Contains(keyword)||o.Summary.Contains(keyword))).OrderByDescending(o => o.CreateTime);
            total = query.Count();
            return query.Skip(start).Take(limit).ToList();
        }

        public IList<CookBook> SelectPageList(int start, int limit, out long total, string cookBookName)
        {
            var query = (from x in this.Session.Query<CookBook>() select x)
               .Where(o => o.IsDelete == false
                   && (string.IsNullOrEmpty(cookBookName) || o.Name.Contains(cookBookName))
               ).OrderByDescending(o => o.EntityIndex)
               .OrderByDescending(o => o.CreateTime);
            total = query.LongCount();

            if (start > total)
            {
                int tempStart = Int32.Parse(total.ToString()) - limit;
                return query.Skip(tempStart).Take(limit).ToList<CookBook>();
            }
            return query.Skip(start).Take(limit).ToList<CookBook>();
        }
    }
}
